import { defineConfig, UserConfig } from "vite";
import { createVitePlugins } from "./build/plugins";
import pkg from "./package.json";
import { resolve } from "path";

import uni from "@dcloudio/vite-plugin-uni";
// 加上下面这一行
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import dayjs from "dayjs";
const { dependencies, devDependencies, name, version } = pkg;
const __APP_INFO__ = {
  pkg: { dependencies, devDependencies, name, version },
  lastBuildTime: dayjs().format("YYYY-MM-DD HH:mm:ss")
};

export default defineConfig((): UserConfig => {
  const root = process.cwd();
  return {
    root,
    server: {
      host: "0.0.0.0",
      port: 8006,
      open: false,
      cors: true
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@import "@/styles/var.scss";`
        }
      }
    },
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src"),
        $api: resolve(__dirname, "./src/api/modules")
      }
    },
    define: {
      __APP_INFO__: JSON.stringify(__APP_INFO__)
    },
    plugins: [
      createVitePlugins(),
      uni(),
      // 加上下面的配置
      AutoImport({
        include: [
          /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/ // .vue
        ],
        //配置后会自动扫描目录下的文件
        dirs: ["src/hooks/**", "src/utils/**", "src/stores/**", "src/api/modules/**", "src/enums/**"],
        imports: ["vue", "uni-app", "pinia"],
        dts: "./src/typings/auto-imports.d.ts"
      }),
      Components({
        // dirs 指定组件所在位置，默认为 src/components
        // 可以让我们使用自己定义组件的时候免去 import 的麻烦
        dirs: ["src/components"],
        // 配置需要将哪些后缀类型的文件进行自动按需引入，'vue'为默认值
        // extensions: ['vue'],
        // // 解析组件，这里以 Element Plus 为例
        // // resolvers: [],
        // // 生成components.d.ts
        dts: "./src/typings/components.d.ts"
        // // 遍历子目录
        // deep: true,
      })
    ],
    build: {
      outDir: "dist",
      minify: "terser",
      // esbuild 打包更快，但是不能去除 console.log，terser打包慢，但能去除 console.log
      // minify: "terser",
      // terserOptions: {
      // 	compress: {
      // 		drop_console: viteEnv.VITE_DROP_CONSOLE,
      // 		drop_debugger: true
      // 	}
      // },
      sourcemap: false,
      // 禁用 gzip 压缩大小报告，可略微减少打包时间
      reportCompressedSize: false,
      // 规定触发警告的 chunk 大小
      chunkSizeWarningLimit: 2000,
      rollupOptions: {
        output: {
          // Static resource classification and packaging
          chunkFileNames: "assets/js/[name]-[hash].js",
          entryFileNames: "assets/js/[name]-[hash].js",
          assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
        }
      }
    }
  };
});
